﻿#region 命名空间

using System;
using System.Text;
using System.Text.RegularExpressions;
using WinnerCMS.Common;
using WinnerCMS.Model;

#endregion

namespace WinnerCMS.BLL.PageFactory.Pages
{
    /// <summary>
    /// 文章模型标签处理
    /// </summary>
    public static class ArticleCommon
    {
        #region 上篇下篇文章

        /// <summary>
        /// 上篇下篇文章
        /// </summary>
        /// <param name="tempStrBuilder"></param>
        /// <param name="articleId"></param>
        public static void PNArticle(StringBuilder tempStrBuilder, int articleId)
        {
            Core[] list = new CoreManage().PreNextContents(articleId);
            if (list[0] != null) //上一篇
            {
                StringBuilder pn = new StringBuilder("<a href=\"");
                pn.Append(list[0].Link);
                pn.Append("\">");
                pn.Append(list[0].ContentTitle);
                pn.Append("</a>");

                tempStrBuilder.Replace("@UP@", pn.ToString());
            }
            else
            {
                tempStrBuilder.Replace("@UP@", "没有文章");
            }
            if (list[1] != null)
            {
                StringBuilder pn = new StringBuilder("<a href=\"");
                pn.Append(list[1].Link);
                pn.Append("\">");
                pn.Append(list[1].ContentTitle);
                pn.Append("</a>");

                tempStrBuilder.Replace("@Down@", pn.ToString());
            }
            else
            {
                tempStrBuilder.Replace("@Down@", "没有文章");
            }
        }

        #endregion

        #region 多少个行分页

        /// <summary>
        /// 多少个行分页
        /// </summary>
        public static int BrNum
        {
            get { return 50; }
        }

        #endregion

        #region 文章被分多少页

        private static readonly Regex HuanHang = new Regex(@"<br\s*?/*?>",
            RegexOptions.IgnoreCase | RegexOptions.Compiled);

        /// <summary>
        /// 文章被分多少页
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static int ArticlePageCount(string content)
        {
            int I = TagModule.TagEngine.TagCollection(content, "Page").Count;
            if (I > 0)
            {
                return I;
            }
            if (content.Contains("@Next@"))
            {
                return content.Split(new[] { "@Next@" }, StringSplitOptions.RemoveEmptyEntries).Length;
            }
            CMSConfig config = CMSConfig.Instance();
            if (config.Article.AutoCutPage == false)
            {
                return 1;
            }
            string[] strArr = HuanHang.Split(content);

            return MyMath.GetCeiling(Convert.ToSingle(strArr.Length), Convert.ToSingle(BrNum));
        }

        #endregion
    }
}